/* 
  create by yjg in 17/11/23 不是很理解
*/
//代码里首先定义了两个类，它们将做为mixins。 可以看到每个类都只定义了一个特定的行为或功能
class Disposable {
  isDisposed: boolean;
  dispose() {
    this.isDisposed = true;
  }
}

class Activatable {
  isActive: boolean;
  activate() {
    this.isActive = true;
  }
  deactivate() {
    this.isActive = false;
  }
}

class SmartObject implements Disposable, Activatable {
  constructor() {
    setInterval(() => console.log(this.isActive + " : " + this.isDisposed), 500);
  }

  interact() {
    this.activate();
  }

  //实现接口的方法
  isDisposed: boolean = false;
  dispose: () => void;
  isActive: boolean = false;
  activate: () => void;
  deactivate: () => void;
}

//最后，把mixins混入定义的类，完成全部实现部分。
applyMixins(SmartObject, [Disposable, Activatable])

let smartObj = new SmartObject();
setTimeout(() => smartObj.interact(), 1000);

//最后，创建这个帮助函数，帮我们做混入操作。 它会遍历mixins上的所有属性，并复制到目标上去，把之前的占位属性替换成真正的实现代码。
function applyMixins(derivedCtor: any, baseCtor: any[]) {
  baseCtor.forEach(baseCtor => {
    Object.getOwnPropertyNames(baseCtor.prototype).forEach(name => {
      derivedCtor.prototype[name] = baseCtor.prototype[name];
    });
  })
}